package com.sha.mumbailocal.server.corn;

import java.io.IOException;
import java.io.Reader;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sha.mumbailocal.server.exception.ConfigurationException;
import com.sha.mumbailocal.server.exception.ContentUnavailableException;
import com.sha.mumbailocal.server.exception.UnparsableContentException;
import com.sha.mumbailocal.server.intf.IContentDataObjectCollection;
import com.sha.mumbailocal.server.intf.IContentParser;
import com.sha.mumbailocal.server.intf.IContentPersister;
import com.sha.mumbailocal.server.intf.IContentProvider;
import com.sha.mumbailocal.server.intf.IContentReaderAndPersisterJob;
import com.sha.mumbailocal.server.intf.IContentTransformer;
import com.sha.mumbailocal.server.model.intf.IPersistentObject;

@SuppressWarnings("serial")
public abstract class AbstractContentPersisterJob extends HttpServlet implements
		IContentReaderAndPersisterJob {

	private IContentProvider contentProvider;

	private IContentParser contentParser;

	private IContentPersister contentPersister;

	private IContentTransformer contentTransformer;

	// private IContentToPojoParser contentToPojoParser;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		Reader reader;
		try {
			reader = contentProvider.getContentReader();
		} catch (ContentUnavailableException e) {
			throw new ServletException(e.getMessage(), e.getCause());
		}

		contentParser.setReader(reader);

		try {
			contentParser.parseContent();
		} catch (UnparsableContentException e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage(), e.getCause());
		}

		IContentDataObjectCollection contentDataObjects = contentParser
				.getContentDataObjects();

		IPersistentObject[] persistentObjects = null;
		try {
			Collection<IPersistentObject> list = contentTransformer
					.transform(contentDataObjects);
			persistentObjects = new IPersistentObject[list.size()]; 
			persistentObjects = list.toArray(persistentObjects);
		} catch (ConfigurationException e) {
			throw new ServletException(e.getMessage(), e.getCause());
		}

		contentPersister.persistDataObjects(persistentObjects);

		//
		// IPersistentObject[] persistentObjects;
		// try {
		// persistentObjects = contentToPojoParser.parseContent();
		// } catch (UnparsableContentException e) {
		// throw new ServletException(e.getMessage(), e.getCause());
		// }

	}

	protected void setContentProvider(IContentProvider contentProvider) {
		this.contentProvider = contentProvider;
	}

	protected void setContentParser(IContentParser contentParser) {
		this.contentParser = contentParser;
	}

	protected void setContentPersister(IContentPersister contentPersister) {
		this.contentPersister = contentPersister;
	}

	@Override
	public IContentProvider getContentProvider() {
		// TODO Auto-generated method stub
		return contentProvider;
	}

	@Override
	public IContentParser getContentParser() {
		// TODO Auto-generated method stub
		return contentParser;
	}

	@Override
	public IContentPersister getContenetPersister() {
		// TODO Auto-generated method stub
		return contentPersister;
	}

	public void setContentTransformer(IContentTransformer contentTransformer) {
		this.contentTransformer = contentTransformer;
	}

}
